<template>
    <div class="box">
        <share-header></share-header>
        <list v-if="show === 2" :tokenData="tokenData" :token="getFileToken" :shareData="shareData"
              :fileList="fileList" :ossKey="ossKey" @folderChange="folderChang"></list>
        <passwordExtract :shareData="shareData" ref="password" v-else-if="show === 1"
                         @approvedOver="approvedOver"></passwordExtract>
        <phone-extract v-else-if="show === 3" :shareData="shareData" @approvedOver="approvedOver"></phone-extract>
    </div>
</template>

<script>
  import ShareHeader from "@/components/ShareHeader.vue";
  import list from "@/components/ExtractSharedFiles/list.vue";
  import passwordExtract from "@/components/ExtractSharedFiles/passwordExtract.vue";
  import phoneExtract from "@/components/ExtractSharedFiles/phone.vue";

  export default {
    components: {
      ShareHeader,
      list,
      passwordExtract,
      phoneExtract,
    },
    data() {
      return {
        shareData: {},
        show: 0,
        passwordObj: {},
        fileList: [],
        tokenData: [],
        getFileToken: ''
      }
    },
    mounted() {
      this.getUser(this.$route.params.code);
    },
    methods: {
      getUser(data) {
        const that = this;
        this.$http.get("/pan/shareInitInfo/" + data).then(function (data) {
          that.shareData = data.data.data;
          console.log(data);
          if (data.data.data.type !== 2) {
            that.checkApproved(data.data.data)
          } else {
            that.show = data.data.data.type;
          }
          that.passwordObj = {
            password: data.data.data.password,
            verification: false
          }
        });
      },
      approvedOver: function (data) {
        if (data.status) {
          this.show = 2
          this.fileList = data.fileList
          this.tokenData = data.tokenData
          this.getFileToken = data.token
          this.ossKey = data.ossKey
        } else {
          this.$message({
            type: 'error',
            message: '提取码错误'
          })
        }
      },
      folderChang:function(data){
        this.fileList = data.fileRelations
        this.tokenData = data.uploadFileTokenVO
        this.getFileToken = data.token
        this.ossKey = data.ossKey

      },
      checkApproved: function (shareData) {
        const password = sessionStorage.getItem(shareData.id)
        if (password && password !== '') {
          this.getFileList(password, shareData)
        } else {
          this.show = shareData.type
        }
      },
      getFileList: function (password, shareData) {
        const that = this
        this.$http.post('/pan//getShareFiles', {
          shareId: shareData.id,
          type: shareData.type,
          verify: password,
          version: "v1"
        }).then(function (data) {
          if (data.data.code === 1) {
            that.fileList = data.data.data.fileRelations
            that.tokenData = data.data.data.uploadFileTokenVO
            that.getFileToken = data.data.data.token
            that.ossKey = data.data.data.ossKey
            that.show = 2
          } else {
            that.show = shareData.type
          }
        })

      }
    }

  };
</script>


<style scoped>
    .box {
        width: 100%;
        height: 100vh;
    }
</style>
